//
//  HomeMeViewController.m
//  TQY
//
//  Created by 甘世清 on 2024/7/7.
//

#import "HomeMeViewController.h"
#import "LoginViewController.h"

#import "AboutViewController.h"
#import "FanKuiViewController.h"
#import "ZhuXiaoViewController.h"
#import "XieYiViewController.h"

#import "InfoSetUpViewController.h"
#import "BlackListViewController.h"
#import "RecommendedView.h"
#import "HomeMeTopView.h"
#import "HomeMeTableViewCell.h"
@interface HomeMeViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic , strong) HomeMeTopView * topView;

@property (nonatomic, strong) RecommendedView * footViews;
@end

@implementation HomeMeViewController
- (void)SQ_viewWillAppear:(BOOL)animated{
    [self SQ_Header];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.zx_hideBaseNavBar = YES;
    [self SQ_Foother];
    self.jsonArray = [[NSMutableArray alloc]initWithObjects:@"黑名单",@"关于",@"反馈",@"协议",@"注销",@"退出", nil];
    [self.tableView reloadData];
    [self SQ_tableViewAddRefresh];

}

- (void)SQ_UIViewForSelfView{
    UIImageView * bgImageView = [[UIImageView alloc]init];
    bgImageView.image = SQGetImage(@"个人中心背景");
    [self.view addSubview:bgImageView];
    [bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableHeaderView = self.topView;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.view);
    }];
   
    NSString * app_recommended = [Manager SQ_stringForKey:@"app_recommended"];
    if ([app_recommended isEqualToString:@"1"]) {
        self.tableView.tableFooterView = self.footViews;
    }
}
 
- (void)SQ_Header{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [HttpTool post:@"user/info" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        [UserModel SQ_updateLocal:httpModel];
        [self.topView SQ_UIView];
        [self.tableView.mj_header endRefreshing];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [self.tableView.mj_header endRefreshing];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
     return self.jsonArray.count;
}
 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HomeMeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HomeMeTableViewCell"];
    if (!cell) {
        cell = [[HomeMeTableViewCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:@"HomeMeTableViewCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
   NSString * name = [self.jsonArray objectAtIndex:indexPath.item];
    cell.nameLabel.text = SQNSLocalString(name);
    cell.leftImageView.image = SQGetImage(name);
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 46;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, 10)];
    view.backgroundColor = UIColor.clearColor;
   
    UIView * minView = [[UIView alloc]initWithFrame:CGRectMake(12, 0, SCREEN_WIDETH-24, 20)];
    minView.layer.cornerRadius = 10;
    minView.backgroundColor = UIColor.whiteColor;
    [view addSubview:minView];
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, 10)];
    view.backgroundColor = UIColor.clearColor;
   
    UIView * minView = [[UIView alloc]initWithFrame:CGRectMake(12, -10, SCREEN_WIDETH-24, 20)];
    minView.layer.cornerRadius = 10;
    minView.backgroundColor = UIColor.whiteColor;
    [view addSubview:minView];
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString * name = [self.jsonArray objectAtIndex:indexPath.item];
    if ([name isEqualToString:@"关于"]) {
        AboutViewController * vc = [[AboutViewController alloc]init];
        [self SQ_pushViewControllerFull:vc];
    }else if ([name isEqualToString:@"反馈"]) {
        FanKuiViewController * vc = [[FanKuiViewController alloc]init];
        [self SQ_pushViewControllerFull:vc];
    }else if ([name isEqualToString:@"协议"]) {
        XieYiViewController * vc = [[XieYiViewController alloc]init];
        [self SQ_pushViewControllerFull:vc];
    }else if ([name isEqualToString:@"注销"]) {
        ZhuXiaoViewController * vc = [[ZhuXiaoViewController alloc]init];
        [self SQ_pushViewControllerFull:vc];
    }else if ([name isEqualToString:@"退出"]) {
        [self SQ_headerButtonClick];
    }else  if ([name isEqualToString:@"黑名单"]) {
        BlackListViewController* vc = [[BlackListViewController alloc]init];
        [self SQ_pushViewControllerFull:vc];
    }
}

-(void)SQ_headerButtonClick{
    WS(weakSelf);
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:SQNSLocalString(@"退出登录") message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction * co0 = [UIAlertAction actionWithTitle:SQNSLocalString(@"确定") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [weakSelf SQ_buttonclick];
    }];
 
    UIAlertAction * co2 = [UIAlertAction actionWithTitle:SQNSLocalString(@"取消") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:co0];
    [alert addAction:co2];
    [self presentViewController:alert animated:YES completion:nil];
}

-(void)SQ_buttonclick{
    [UserModel clearLocalUserModel];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[[LoginViewController alloc]init]];
     self.view.window.rootViewController = nav;
}



- (RecommendedView *)footViews{
    if (!_footViews) {
        _footViews = [[RecommendedView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, 70)];
    }
    return _footViews;
}
- (HomeMeTopView *)topView{
    if (!_topView) {
        _topView = [[HomeMeTopView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, 290+TopHeight)];
    }
    return _topView;
}
@end
